package com.easy.controller;

import com.easy.bean.Favorites;
import com.easy.service.FavoritesService;
import com.easy.util.Page;
import com.easy.util.Result;
import com.easy.util.SYS;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin // 允许跨域访问
@RequestMapping("/favorites")
public class FavoritesController {
    @Autowired
    FavoritesService favoritesService;

    @PostMapping
    public Result add(HttpServletRequest request, @RequestBody Favorites favorites){
        // 获取当前用户信息
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
        Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

        // 设置收藏创建者为当前用户
        if (idObj != null) {
            favorites.setUser_id(Integer.valueOf(idObj.toString()));
        }
        Object obj = favoritesService.add(favorites);
        return Result.success(obj);
    }

    @PutMapping
    public Result edit(HttpServletRequest request, @RequestBody Favorites existingFavorites, @RequestBody Favorites favorites){
        // 获取当前用户信息
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
        Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

        // 管理员可以编辑所有收藏，普通用户只能编辑自己的收藏
        if (roleObj != null && !"admin".equals(roleObj.toString())) {
            if (idObj != null) {
                Integer userId = Integer.valueOf(idObj.toString());
                // 检查收藏是否属于当前用户
                Favorites existingFavorite = favoritesService.one(favorites.getId());
                if (existingFavorite != null && !existingFavorite.getUser_id().equals(userId)) {
                    return Result.fail(403, "只能编辑自己的收藏");
                }
            }
        }
        Object obj = favoritesService.edit(favorites);
        return Result.success(obj);
    }

    @DeleteMapping("/{id}")
    public Result del( HttpServletRequest request,@PathVariable("id") Integer id){
        // 获取当前用户信息
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
        Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

        // 管理员可以删除所有收藏，普通用户只能删除自己的收藏
        if (roleObj != null && !"admin".equals(roleObj.toString())) {
            if (idObj != null) {
                Integer userId = Integer.valueOf(idObj.toString());
                // 检查收藏是否属于当前用户
                Favorites existingFavorite = favoritesService.one(id);
                if (existingFavorite != null && !existingFavorite.getUser_id().equals(userId)) {
                    return Result.fail(403, "只能删除自己的收藏");
                }
            }
        }
        Object obj = favoritesService.del(id);
        return Result.success(obj);
    }

    @GetMapping("/{id}")
    public Result one(@PathVariable("id") Integer id){

        Object obj = favoritesService.one(id);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(Favorites favorites){
        Object obj = favoritesService.all(favorites);
        return Result.success(obj);
    }

    @GetMapping("page")
    public Result page( HttpServletRequest request,Page page, Favorites favorites){
        // 获取当前用户信息
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
        Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

        // 普通用户只能查看自己的收藏
        if (roleObj != null && !"admin".equals(roleObj.toString())) {
            if (idObj != null) {
                favorites.setUser_id(Integer.valueOf(idObj.toString()));
            }
        }
        // 查询一共有多少条数据
        // 查询当前页要显示的数据
        // 将以上数据封装到page对象中
        // 返回page对象
        favoritesService.page(page, favorites);
        return Result.success(page);
    }
}
